package org.aeon.newsmanagersystem.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.aeon.newsmanagersystem.dao.CommentsMapper;
import org.aeon.newsmanagersystem.dao.NewsMapper;
import org.aeon.newsmanagersystem.pojo.News;
import org.aeon.newsmanagersystem.pojo.Topic;
import org.aeon.newsmanagersystem.pojo.other.PageInfo;
import org.aeon.newsmanagersystem.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Set;
import java.util.UUID;

/**
 * @author: MissingAeon
 * @date: 2024/3/27 17:30
 * @description:
 */
@Slf4j
@Service
public class NewsServiceImpl implements NewsService {

    private final NewsMapper newsMapper;
    private final CommentsMapper commentsMapper;

    @Autowired
    public NewsServiceImpl(NewsMapper newsMapper, CommentsMapper commentsMapper) {
        this.newsMapper = newsMapper;
        this.commentsMapper = commentsMapper;
    }

    private static String imagesHandle(MultipartFile file) {
        try {
            final String contentType = file.getContentType();
            // 允许的图片格式
            final Set<String> imageSet = Set.of("image/png", "image/jpg", "image/jpeg", "image/gif");
            String path = null;
            if (imageSet.contains(contentType)) {
                // 图片保存
                assert contentType != null;
                final String prefix = "/upload";
                final File folder = new File(prefix);
                if (!folder.exists()) {
                    if (!folder.mkdirs()) {
                        log.error("创建文件夹失败！");
                        return null;
                    }
                }
                path = prefix + "/" + UUID.randomUUID() + '_' + file.getName() +
                        "." + contentType.substring(contentType.length() - 3);
                File imageFile = new File(path);// 上传后的文件保存目录及名字
                file.transferTo(imageFile);// 将上传文件保存到相应位置
            }
            return path;
        } catch (IOException | IllegalStateException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }


    @Override
    public PageInfo<News, Topic> page(PageInfo<News, Topic> page) {
        final Topic condition = page.getCondition();
        if (condition != null) {
            final Integer tid = condition.getTid();
            page.setList(newsMapper.getByTopicIdIfAnyLimited(tid, page.getStart(), page.getLimit()));
            page.setTotal(newsMapper.countByTopicIdIfAny(tid));
        } else {
            page.setList(newsMapper.getByTopicIdIfAnyLimited(null, page.getStart(), page.getLimit()));
            page.setTotal(newsMapper.countByTopicIdIfAny(null));
        }
        return page;
    }

    @Override
    public boolean exists(Topic topic) {
        return newsMapper.countByTopicIdIfAny(topic.getTid()) > 0;
    }

    @Override
    public boolean add(News news, MultipartFile file) {
        final String path = imagesHandle(file);

        // 消息对象
        news.setNpicpath(path);
        news.setNcreatedate(new Date());
        return newsMapper.insert(news) == 1;
    }

    @Override
    public News get(Integer nid) {
        return newsMapper.selectByPrimaryKey(nid);
    }

    @Override
    @Transactional
    public int delete(Integer nid) {
        // 删除评论
        commentsMapper.delByNid(nid);
        // 删除新闻
        return newsMapper.deleteByPrimaryKey(nid);
    }

    @Override
    public boolean modify(News news, MultipartFile file) {
        final String path = imagesHandle(file);

        // 消息对象
        news.setNpicpath(path);
        news.setNmodifydate(new Date());
        return newsMapper.updateByPrimaryKey(news) == 1;
    }
}
